package com.hanson.api;

import com.alibaba.fastjson.JSONObject;
import com.hanson.common.bean.ResultMessage;
import com.hanson.common.bean.SearchBean;
import com.hanson.common.util.BusinessUtil;
import com.hanson.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@Api(tags = "首页数据管理")
@RestController
@RequestMapping("/api/index")
public class IndexApiController {
    @Autowired
    private IndexSlideService indexSlideService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private SearchSortService searchSortService;
    @Autowired
    private IndexNavService indexNavService;
    @Autowired
    private SeckillGoodsService seckillGoodsService;
    @Autowired
    private GroupGoodsService groupGoodsService;
    @Autowired
    private CouponsService couponsService;

    @ApiOperation(value = "获取轮播图")
    @RequestMapping(value = "/getSlides", method = RequestMethod.GET)
    public ResultMessage getSlides() throws Exception {
        return ResultMessage.success(indexSlideService.selectAll(), null);
    }

    @ApiOperation(value = "获取导航图表")
    @RequestMapping(value = "/getNavs", method = RequestMethod.GET)
    public ResultMessage getNavs() throws Exception {
        return ResultMessage.success(indexNavService.selectAll(), null);
    }

    @ApiOperation(value = "获取热销商品")
    @RequestMapping(value = "/getHotGoods/{page}/{limit}", method = RequestMethod.GET)
    public ResultMessage getHotGoods(@PathVariable Integer page, @PathVariable Integer limit) throws Exception {
        ModelMap modelMap = new ModelMap();
        SearchBean hotBean = new SearchBean();
        hotBean.setIsHot(1);
        return ResultMessage.success(goodsService.getSimpleGoodsList(page, limit, hotBean, modelMap), null);
    }

    @ApiOperation(value = "获取促销商品")
    @RequestMapping(value = "/getBenefitGoods/{page}/{limit}", method = RequestMethod.GET)
    public ResultMessage getBenefitGoods(@PathVariable Integer page, @PathVariable Integer limit) throws Exception {
        ModelMap modelMap = new ModelMap();
        SearchBean benefitBean = new SearchBean();
        benefitBean.setIsBenefit(1);
        return ResultMessage.success(goodsService.getSimpleGoodsList(page, limit, benefitBean, modelMap), null);
    }

    @ApiOperation(value = "分页获取海量精品商品")
    @RequestMapping(value = "/getBestGoods/{page}/{limit}", method = RequestMethod.GET)
    public ResultMessage getBestGoods(@PathVariable Integer page, @PathVariable Integer limit) throws Exception {
        ModelMap modelMap = new ModelMap();
        SearchBean bestBean = new SearchBean();
        bestBean.setIsBest(1);
        return ResultMessage.success(goodsService.getSimpleGoodsList(page, limit, bestBean, modelMap), null);
    }

    @ApiOperation(value = "获取推荐商品 所有有推荐的商品的都用这个接口")
    @RequestMapping(value = "/getRecommendGoods", method = RequestMethod.GET)
    public ResultMessage getRecommendGoods() throws Exception {
        return ResultMessage.success(goodsService.getRecommendGoods(), null);
    }

    @ApiOperation(value = "获取搜索页面推荐关键字")
    @RequestMapping(value = "/getRecommendSearch", method = RequestMethod.GET)
    public ResultMessage getRecommendSearch() throws Exception {
        return ResultMessage.success(searchSortService.findZset(0, 5), null);
    }

    @ApiOperation(value = "根据关键字和条件搜索商品")
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public ResultMessage search(@RequestBody SearchBean searchBean) throws Exception {
        if (StringUtils.isNotEmpty(searchBean.getKeyWords())) {
            searchSortService.sort(searchBean.getKeyWords());
        }
        ModelMap modelMap = new ModelMap();
        return ResultMessage.success(goodsService.getSimpleGoodsList(0, 1000, searchBean, modelMap), null);
    }

    @ApiOperation(value = "根据类型搜索商品")
    @RequestMapping(value = "/searchType/{typeId}", method = RequestMethod.GET)
    public ResultMessage search(@PathVariable String typeId) throws Exception {
        ModelMap modelMap = new ModelMap();
        return ResultMessage.success(goodsService.getSimpleGoodsByType(typeId, modelMap), null);
    }

    @ApiOperation(value = "获取秒杀结束时间")
    @RequestMapping(value = "/getSurplusSecond", method = RequestMethod.GET)
    public ResultMessage getSurplusSecond() throws Exception {
        return ResultMessage.success(seckillGoodsService.getSurplusSecond(), null);
    }

    @ApiOperation(value = "获取首页秒杀商品")
    @RequestMapping(value = "/getSkillGoods", method = RequestMethod.GET)
    public ResultMessage getSkillGoods() throws Exception {
        ModelMap modelMap = new ModelMap();
        return ResultMessage.success(seckillGoodsService.getIndexSkillGoods(), null);
    }

    @ApiOperation(value = "获取首页拼团商品")
    @RequestMapping(value = "/getGroupGoods", method = RequestMethod.GET)
    public ResultMessage getGroupGoods() throws Exception {
        return ResultMessage.success(groupGoodsService.getGroupGoodsPage(1, 4).getRecords(), null);
    }

    @ApiOperation(value = "获取首页优惠价列表")
    @RequestMapping(value = "/getCoupons", method = RequestMethod.GET)
    public ResultMessage getCoupons(HttpServletRequest request) throws Exception {
        return ResultMessage.success(couponsService.getAllCoupons(BusinessUtil.getHeaderOpenId(request)), null);
    }

    @ApiOperation(value = "获取优惠卷")
    @RequestMapping(value = "/receiveCoupons/{id}", method = RequestMethod.POST)
    public ResultMessage receiveCoupons(@PathVariable(value = "id") String id, HttpServletRequest request) throws Exception {
        couponsService.receiveCoupons(BusinessUtil.getHeaderOpenId(request), id);
        return ResultMessage.success(null, "领取优惠卷成功");
    }

}
